
import '../http/base_http_client.dart';
import '../http/models/gitcode_user.dart';

class ApiService {
  final BaseHttpClient _httpClient;
  final accessToken = '------------------';

  ApiService(this._httpClient);

  void initialize() {
      _httpClient.setBaseUrl('https://api.gitcode.com/api/v5');

      _httpClient.setHeaders({
        'Content-Type': 'application/json',
      });

  }

  // 获取用户信息
  Future<GitCodeUser> getUserInfo(String username) async {
    try {
      final response = await _httpClient.get(
        '/users/$username',
        queryParameters: {
          'access_token': accessToken,
        },
      );

      return GitCodeUser.fromJson(response.data);
    } catch (e) {
      print('获取用户信息失败: $e');
      rethrow;
    }
  }

  // 获取用户仓库列表
  Future<List<dynamic>> getUserRepos(String username) async {
    try {
      final response = await _httpClient.get(
        '/users/$username/repos',
        queryParameters: {
          'access_token': accessToken,
          'sort': 'updated',
          'per_page': 10,
        },
      );

      return response.data as List;
    } catch (e) {
      print('获取用户仓库失败: $e');
      rethrow;
    }
  }

  // 搜索用户
  Future<List<dynamic>> searchUsers(String query) async {
    try {
      final response = await _httpClient.get(
        '/search/users',
        queryParameters: {
          'access_token': 'SE7Gx6zoWYa4vmysQD6H1hgV',
          'q': query,
          'per_page': 20,
        },
      );

      return response.data['items'] as List;
    } catch (e) {
      print('搜索用户失败: $e');
      rethrow;
    }
  }
}
